﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace Marking.Dashboard.Infrastructures
{
    public static class CycleTypeHelper
    {
        private static Dictionary<string, CycleType> _CycleTypeMaps = new Dictionary<string, CycleType>
        {
            { "日", new DayCycleType() },
            { "月", new MonthCycleType() },
            { "年", new YearCycleType() }
        };

        public static IEnumerable<string> CircleTypes
        {
            get
            {
                return _CycleTypeMaps.Keys;
            }
        }

        public static DateTime GetDefaultStartDate(string cycleType, DateTime endDate)
        {
            return _CycleTypeMaps[cycleType].GetDefaultStartDate(endDate);
        }

        public static string GetDisplayDateString(string cycleType, DateTime date)
        {
            return _CycleTypeMaps[cycleType].GetDisplayDateString(date);
        }

        public static IEnumerable<DateTime> SimulateDates(string cycleType, DateTime startDate, DateTime endDate)
        {
            return _CycleTypeMaps[cycleType].SimulateDates(startDate, endDate);
        }

        private abstract class CycleType
        {
            public abstract DateTime GetDefaultStartDate(DateTime endDate);

            public abstract string GetDisplayDateString(DateTime date);

            public abstract IEnumerable<DateTime> SimulateDates(DateTime startDate, DateTime endDate);
        }

        private class YearCycleType : CycleType
        {
            public override DateTime GetDefaultStartDate(DateTime endDate)
            {
                return endDate.AddMonths(-12 * 3);
            }

            public override string GetDisplayDateString(DateTime date)
            {
                return date.ToString("yyyy");
            }

            public override IEnumerable<DateTime> SimulateDates(DateTime startDate, DateTime endDate)
            {
                for (var i = startDate; i <= endDate; i = i.AddYears(1))
                {
                    yield return i;
                }
            }
        }

        private class MonthCycleType : CycleType
        {
            public override DateTime GetDefaultStartDate(DateTime endDate)
            {
                return endDate.AddMonths(-12);
            }

            public override string GetDisplayDateString(DateTime date)
            {
                return date.ToString("yyyy-MM");
            }

            public override IEnumerable<DateTime> SimulateDates(DateTime startDate, DateTime endDate)
            {
                for (var i = startDate; i <= endDate; i = i.AddMonths(1))
                {
                    yield return i;
                }
            }
        }

        private class DayCycleType : CycleType
        {
            public override DateTime GetDefaultStartDate(DateTime endDate)
            {
                return endDate.AddDays(-15);
            }

            public override string GetDisplayDateString(DateTime date)
            {
                return date.ToString("MM-dd");
            }

            public override IEnumerable<DateTime> SimulateDates(DateTime startDate, DateTime endDate)
            {
                for (var i = startDate; i <= endDate; i = i.AddDays(1))
                {
                    yield return i;
                }
            }
        }
    }
}
